package com.mall.manager.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mall.untils.Page;
import com.mall.untils.StringUtil;

@Service
public class GardenService {
	Logger logger=LoggerFactory.getLogger(GardenService.class);
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	
	public Page queryPage(Map<String, Object> paraMap) {
		Page page = null;
		List<Map<String, Object>> queryForList = null;
		int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
		String name = MapUtils.getString(paraMap, "name");  //TODO 提供园所名称
		String id = MapUtils.getString(paraMap, "id");  
		String garden_type = MapUtils.getString(paraMap, "garden_type");//园所类型
		int pageSize = 10; //每页显示10条
		Integer total=0; //总数
		
		try {
			if (pageNo == 0) {
				pageNo = 1;
			}
//			pageNo = 2;
			StringBuffer sqlCount = new StringBuffer("select count(*) from db_processing.t_garden where 1=1 ");
			
			if (StringUtils.isNotBlank(name)) {
				sqlCount.append(" and garden_name like ? ");
			}
			
			if (StringUtils.isNotBlank(id)) {
				sqlCount.append(" and id = ? ");
			}
			
			if(StringUtils.isNotBlank(garden_type)){
				sqlCount.append(" and garden_type = ? ");
			}
			
			
			if(StringUtils.isBlank(name) && StringUtils.isBlank(garden_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class);
			}
			if(StringUtils.isBlank(name) && StringUtils.isNotBlank(garden_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class,garden_type);
			}
			if(StringUtils.isNotBlank(name) && StringUtils.isBlank(garden_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class,"%"+StringUtil.getByteToString(name)+"%");
			}
			if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(garden_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class,"%"+StringUtil.getByteToString(name)+"%",garden_type);
			}
			
			
			
			int startIndex = Page.getStartOfPage(pageNo, pageSize);
			StringBuffer sql = new StringBuffer("select * from db_processing.t_garden where 1=1 ");
			if (StringUtils.isNotEmpty(name)) {
				sql.append("and garden_name like ? ");
			}
			if (StringUtils.isNotEmpty(id)) {
				sqlCount.append(" and id = ? ");
			}
			if(StringUtils.isNotBlank(garden_type)){
				sqlCount.append(" and garden_type = ? ");
			}
			
			String sqlbf = sql.append(" order by id desc limit " + startIndex + "," + pageSize).toString();
			
			if (StringUtils.isNotEmpty(name)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, "%"+StringUtil.getByteToString(name)+"%");
			}else
			if (StringUtils.isNotEmpty(id)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, id);
			}else
			if (StringUtils.isNotEmpty(id)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, garden_type);
			}else{
				queryForList = jdbcTemplate.queryForList(sqlbf);
			}
			page = new Page(startIndex, total, pageSize, queryForList);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
			return null;
		}
	}

}
